﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Queue que = new Queue();

        Socket ck;
        


        /// <summary>
        /// 连接
        /// </summary>
        private void btnConnection_Click(object sender, EventArgs e)
        {
            IPAddress ip = IPAddress.Parse(txtIp.Text.Trim());
            IPEndPoint point = new IPEndPoint(ip, int.Parse(txtPort.Text));

            try
            {
                ck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ck.Connect(point);

                txtReceive.Text = "服务器" + ck.RemoteEndPoint.ToString() + ",客户端:" + ck.LocalEndPoint.ToString();

                Thread th = new Thread(ReceiveMsg);
                th.IsBackground = true;
                th.Start();
            }
            catch (Exception)
            {
                
                throw;
            }
           
        }

        /// <summary>
        /// 断开
        /// </summary>
        private void btnBreak_Click(object sender, EventArgs e)
        {
            ck.Close();
        }

        /// <summary>
        /// 重连
        /// </summary>
        private void btnAgen_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 发送
        /// </summary>
        private void benSend_Click(object sender, EventArgs e)
        {
            que.Enqueue(txtSend.Text.Trim());
        }



        /// <summary>
        /// 接收
        /// </summary>
        void ReceiveMsg()
        {
            while (true)
            {
                try
                {
                     byte[] buffer = new byte[1024 * 1024];
                     int n = ck.Receive(buffer);
                     string s = Encoding.UTF8.GetString(buffer, 0, n);
                     this.txtReceive.Text = ck.RemoteEndPoint.ToString() + ":" + s;
                }
                catch (Exception ex)
                {
                    throw;
                }
                Thread.Sleep(5);
            }
        }


        /// <summary>
        /// 发送
        /// </summary>
        void SendMsg()
        {
            while (true)
            {
                try
                {
                    if (que.Count > 0)
                    {
                        byte[] sendByte = System.Text.Encoding.ASCII.GetBytes(que.Dequeue().ToString());
                        ck.Send(sendByte);
                    }
                }
                catch (Exception)
                {
                    
                    throw;
                }
                Thread.Sleep(5);
            }
        }





    }
}
